package com.rs.controller.Rs;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


import com.rs.model.Department;
import com.rs.service.rs.DepartmentService;
import com.rs.util.SpringContextUtils;


public class BumenJFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	class AddBumen extends BumenJFrame {
		private JPanel contentPane;

		private static final long serialVersionUID = 1L;

		private JTextField names;//部门名称
		
		private DepartmentService dts = SpringContextUtils.getBean(DepartmentService.class);
		
		public AddBumen() {
			super();
			//设置标题
			setTitle("添加部门");
			//设置大小
			setBounds(250, 50, 300, 200);
			//主体
			contentPane = new JPanel();
			setContentPane(contentPane);
			contentPane.setLayout(null);

			//创建1个字体实bai例
			Font font = new Font("宋体", Font.PLAIN, 15);

			//部门名称	
			JLabel name = new JLabel("新部门名称：");
			name.setFont(font);
			name.setBounds(50, 25, 120, 20);
			names = new JTextField();
			names.setBounds(140, 25, 100, 20);
			names.setColumns(10);
			JButton tijiao = new JButton("提交");
			tijiao.setFont(font);
            tijiao.setBounds(110, 100, 70, 20);
            contentPane.add(name);
			contentPane.add(names);
            contentPane.add(tijiao);
            tijiao.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				Department dd = new Department();
    				String de_name = names.getText();
    				dd.setDe_name(de_name);
    				String result = dts.addBuMen(dd);
    				if(result.equals("添加成功")){
    					showMessageDialog("添加成功");
    				}else {
    					showMessageDialog("添加失败");
    				}
    			}
    		});
		}	
	}
	class SelBumen extends BumenJFrame {
		private JPanel contentPane;
		private DepartmentService dts = SpringContextUtils.getBean(DepartmentService.class);
		private static final long serialVersionUID = 1L;
	public SelBumen(List<Department> ll) {
		super();
		
		//创建1个字体实bai例
	    Font font = new Font("宋体", Font.PLAIN, 15);		
		//设置标题
		setTitle("部门列表");
		//设置大小
		setBounds(250, 50, 400, 500);
		//主体
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(20, 20,400,300));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("编号");
		label.setFont(font);
		label.setBounds(35,25,70,20);
		contentPane.add(label);
		
		JLabel label1 = new JLabel("部门名称");
		label1.setFont(font);
		label1.setBounds(105,25,100,20);
		contentPane.add(label1);
		
		JLabel label2 = new JLabel("操作");
		label2.setFont(font);
		label2.setBounds(264,25,70,20);
		contentPane.add(label2);
		
		for(int i=0;i<ll.size();i++) {
			
			final Department l1 = ll.get(i);
			JLabel label3 = new JLabel(l1.getDe_id()+"");
			label3.setFont(font);
			label3.setBounds(35,25*(i+2),70,20);
			contentPane.add(label3);
			
			JLabel label4 = new JLabel(l1.getDe_name());
			label4.setFont(font);
			label4.setBounds(105,25*(i+2),100,20);
			contentPane.add(label4);
			
			JButton del = new JButton("删除");
			del.setFont(font);
			del.setBounds(205,25*(i+2),70,20);
			contentPane.add(del);
			del.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				Object[] options = {"是(Y)","否(N)"};
    	            int a = JOptionPane.showOptionDialog(null,"您确定要删除这个部门吗？","提醒",
    	            JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
    	            if(a==1) {
    	            	return;
    	            }else if(a==0){
    	            	String result = dts.delBuMen(l1.getDe_id());
        				List<Department> l2 = dts.selBuMenAll();
        				if(result.equals("删除成功")){
        					showMessageDialog("删除成功");
        					dispose();
        					new BumenJFrame().new SelBumen(l2).setVisible(true);
        				}else {
        					showMessageDialog("删除失败");
        				}
    	            }
    				
    			}
    		});
			
			JButton update = new JButton("编辑");
			update.setFont(font);
			update.setBounds(275,25*(i+2),70,20);
			contentPane.add(update);
			update.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				String title = JOptionPane.showInputDialog(null, "请输入：\n", "修改部门名称", JOptionPane.PLAIN_MESSAGE);
    				l1.setDe_name(title);
    				String result = dts.UpdateBuMen(l1);
    				List<Department> l2 = dts.selBuMenAll();
    				if(result.equals("编辑成功")){
    					showMessageDialog("编辑成功");
    					dispose();
    					new BumenJFrame().new SelBumen(l2).setVisible(true);
    					
    				}else {
    					showMessageDialog("编辑失败");
    				}
    			}
    		});
				
		}
	    }
	}
	
	//提示弹窗
	 public void showMessageDialog(String massage){
	  JOptionPane.showMessageDialog(this,massage);
	 }
	
	
}
